<script>
	$(function() {
		$( ".datepicker" ).datepicker();
	});
        
        $(document).ready(function(){
            var id_kamar = $( "#id-kamar" ).val();
            $("#jenis-pengenal").change(function() {
                    $.post("<?php echo DATACUSTOMER ?>get_data_customer_autocomplete", "no_pengenal="+$("#no-pengenal").val()+"&&jenis_pengenal="+$("#jenis-pengenal").val(), function(data){
                        var arr = $.parseJSON(data);
                        if (arr['no_pengenal']!='')
                            {
                                $("#country-of-passport").val(arr['country_of_passport']);
                                $("#first-name").val(arr['first_name']);
                                $("#last-name").val(arr['last_name']);
                                $("#status-data-customer").val(arr['id']);
                            }
                            else
                                $("#status-data-customer").val('-');
                        });
                });
            $("#no-pengenal").keyup(function() {
                
                    $.post("<?php echo DATACUSTOMER ?>get_data_customer_autocomplete", "no_pengenal="+$("#no-pengenal").val()+"&&jenis_pengenal="+$("#jenis-pengenal").val(), function(data){
                        var arr = $.parseJSON(data);
                        if (arr['no_pengenal']!='')
                            {
                                $("#country-of-passport").val(arr['country_of_passport']);
                                $("#first-name").val(arr['first_name']);
                                $("#last-name").val(arr['last_name']);
                                $("#status-data-customer").val(arr['id']);
                            }
                            else
                                $("#status-data-customer").val('-');
                        });
                });
            $("#date-pesan-keluar").change(function() {
                    var jumlah_hari = parseInt($( "#jumlah-hari" ).val());
                    var date_masuk = new Date($("#date-pesan-masuk").val());
                    var date_keluar = new Date($("#date-pesan-keluar").val());
                    var d = date_masuk.getDate();
                    var m = date_masuk.getMonth();
                    var y = date_masuk.getYear();
//                    var NextDate= new Date((1900+y), (d+jumlah_hari), (m));
//                    alert(NextDate.getYear());
                    
                    var tanggal_masuk = (1900+y)+"-"+(m+1)+"-"+d;
//                    alert(tanggal_masuk);
                    var tanggal_keluar= (1900+date_keluar.getYear())+"-"+(date_keluar.getMonth()+1)+"-"+date_keluar.getDate();
                    $( "#jumlah-hari" ).val(days_between(date_masuk, date_keluar));
                    $('#adjoin-room').empty();
                    $.post("<?php echo KAMAR ?>adjoining_room_status", "id_kamar="+id_kamar+"&&tanggalmasuk="+tanggal_masuk+"&&tanggalkeluar="+tanggal_keluar, function(data){
                        var arr = $.parseJSON(data);
                        var temp = '';
                        
                        for(var i=0;i<arr['adjoining'].length;i++)
                            {
                                
                                if(id_kamar==arr['all'][i]['id_room1'])
                                    temp = arr['all'][i]['id_room2'];
                                else
                                    temp = arr['all'][i]['id_room1'];
                                if(arr['adjoining'][i]['used']==true)
                                    $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='"+ temp +"'>"+ arr['adjoining'][i]["nama_kamar1"] +" - "+ arr['adjoining'][i]["nama_kamar2"] +"</input> waiting list<br/>");
                                else 
                                    $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='"+ temp +"'>"+ arr['adjoining'][i]["nama_kamar1"] +" - "+ arr['adjoining'][i]["nama_kamar2"] +"</input><br/>");
                                    
                            }
                            $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='-'>none</input><br/>");
                        });  
                });
                $( "#jumlah-hari" ).keyup(function() {
                    var jumlah_hari = parseInt($( "#jumlah-hari" ).val());
                    var date_masuk = new Date($("#date-pesan-masuk").val())
                    var d = date_masuk.getDate();
                    var m = date_masuk.getMonth();
                    var y = date_masuk.getYear();
//                    var NextDate= new Date((1900+y), (d+jumlah_hari), (m));
                    var NextDate= new Date();
                    NextDate.setDate(date_masuk.getDate()+jumlah_hari);
//                    alert(NextDate.getYear());
                    $("#date-pesan-keluar").val((NextDate.getMonth()+1)+"/"+NextDate.getDate()+"/"+(1900+NextDate.getYear()));
                    var tanggal_masuk = (1900+y)+"-"+(m+1)+"-"+d;
//                    alert(tanggal_masuk);
                    var tanggal_keluar= (1900+NextDate.getYear())+"-"+(NextDate.getMonth()+1)+"-"+NextDate.getDate();
                    $('#adjoin-room').empty();
                    $.post("<?php echo KAMAR ?>adjoining_room_status", "id_kamar="+id_kamar+"&&tanggalmasuk="+tanggal_masuk+"&&tanggalkeluar="+tanggal_keluar, function(data){
                        var arr = $.parseJSON(data);
                        var temp = '';
                        
                        for(var i=0;i<arr['adjoining'].length;i++)
                            {
                                
                                if(id_kamar==arr['all'][i]['id_room1'])
                                    temp = arr['all'][i]['id_room2'];
                                else
                                    temp = arr['all'][i]['id_room1'];
                                if(arr['adjoining'][i]['used']==true)
                                    $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='"+ temp +"'>"+ arr['adjoining'][i]["nama_kamar1"] +" - "+ arr['adjoining'][i]["nama_kamar2"] +"</input> waiting list<br/>");
                                else 
                                    $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='"+ temp +"'>"+ arr['adjoining'][i]["nama_kamar1"] +" - "+ arr['adjoining'][i]["nama_kamar2"] +"</input><br/>");
                                    
                            }
                            $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='-'>none</input><br/>");
                        });  
                });
		
	});
	</script>
<form action="<?php echo HOME ?>waitinglist_kamar_process">
    <h3 class="center">DATA PELANGGAN</h3>
    <table class="tabelket" width="500px">
        <tr>
            <td class="right" width="35%">Nama kamar</td>
            <td class="left" width="65%">: <?php echo $data_kamar['nama_kamar'] ?></td>
        </tr>
    <input type="hidden" name="id_kamar" id="id-kamar" value="<?php echo $data_kamar['id_kamar'] ?>"/>
        <tr>
            <td class="right"><?php echo $data_kamar['tipe_letak_kamar'] ?></td>
            <td class="left">: <?php echo $data_kamar['no_letak_kamar'] ?></td>
        </tr>
        <tr>
            <td class="right">Nomor</td>
            <td class="left">: <?php echo $data_kamar['no_kamar'] ?></td>
        </tr>
        <tr>
            <td class="right">Nomor pengenal</td>
            <td class="left">: <input name="no_pengenal" class="panjang" id="no-pengenal" value="<?php if ($customer != 0) echo $data_customer['no_pengenal'] ?>"/></td>
        <input name="status_data_customer" type="hidden" id="status-data-customer" value="-" />
        </tr>
        <tr>
            <td class="right">Jenis pengenal</td>
            <td class="left">: <select name="jenis_pengenal" id="jenis-pengenal">
                    <option <?php if ($customer != 0 && $data_customer['jenis_pengenal'] == "ktp") echo "selected" ?>>ktp</option>
                    <option <?php if ($customer != 0 && $data_customer['jenis_pengenal'] == "sim") echo "selected" ?>>sim</option>
                    <option <?php if ($customer != 0 && $data_customer['jenis_pengenal'] == "passport") echo "selected" ?>>passport</option>
                </select></td>
        </tr>
    <input name="status_data_customer" type="hidden" id="status-data-customer" value="-" />
        <tr>
            <td class="right">Country of passport</td>
            <td class="left">: 
                <input class="panjang" id="country-of-passport" name="country_of_passport" value="<?php if ($customer != 0) echo $data_customer['country_of_passport'] ?>"/>
            </td>
        </tr>
        <tr>
            <td class="right">First name</td>
            <td class="left">: 
                <input class="panjang" id="first-name" name="first_name" value="<?php if ($customer != 0) echo $data_customer['first_name'] ?>"/>
            </td>
        </tr>
        <tr>
            <td class="right">Last name</td>
            <td class="left">: 
                <input class="panjang" id="last-name"  name="last_name" value="<?php if ($customer != 0) echo $data_customer['last_name'] ?>"/>
            </td>
        </tr>
    <input type="hidden" name="id_data_customer" value="<?php if($customer!=0) echo $data_customer['id']; else echo 0?>">
    <input type="hidden" name="id_waitinglist_to" value="<?php echo $customer_id ?>">
        <tr>
            <td class="right">Jumlah hari pesan</td>
            <td class="left">: <input type="text" class="pendek" id="jumlah-hari" /></td>
        </tr>
        <?php if($customer!=0){ 
            $temp = explode("-", $customer['date_pesan_masuk']);
            $temp2 = explode("-", $customer['date_pesan_keluar']);
            ?>
            <tr>
                <td class="right">Tanggal pesan masuk</td>
                <td class="left">: <input type="text" class="datepicker" id="date-pesan-masuk" name="date_pesan_masuk" value="<?php echo $temp[1] . "/" . $temp[2] . "/" . $temp[0] ?>"></td>
            </tr>
            <tr>
                <td class="right">Tanggal pesan keluar</td>
                <td class="left">: <input type="text" class="datepicker" id="date-pesan-keluar" name="date_pesan_keluar" value="<?php echo $temp2[1] . "/" . $temp2[2] . "/" . $temp2[0] ?>"></td>
            </tr>
        <?php } else { ?>
            <tr>
                <td class="right">Tanggal pesan masuk</td>
                <td class="left">: <input type="text" class="datepicker" id="date-pesan-masuk" name="date_pesan_masuk" value="<?php echo ($bulan + 1) . "/" . $tanggal . "/" . date("Y") ?>"></td>
            </tr>
            <tr>
                <td class="right">Tanggal pesan keluar</td>
                <td class="left">: <input type="text" class="datepicker" id="date-pesan-keluar" name="date_pesan_keluar" value="<?php echo($bulan + 1) . "/" . $tanggal . "/" . date("Y") ?>"></td>
            </tr>
        <?php } ?>
        <tr>
            <td class="right">Jumlah orang menginap</td>
            <td class="left">: <input type="text" class="pendek" name="jumlah_huni" value="<?php if ($customer != 0) echo $customer['jumlah_huni'] ?>"/></td>
        </tr>
        <input type="hidden" name="id_customer" value="<?php if($customer!=0) echo $customer['id']; else echo 0?>">
        <tr>
            <td class="right">Jenis pembayaran</td>
            <td class="left">: <select name="pembayaran_trax">
                    <option>Tunai</option>
                    <option>Check</option>
                </select></td>
        </tr>
        <tr>
            <td class="right">Jumlah</td>
            <td class="left">: <input name="harga_trax" /></td>
        </tr>
        <tr>
            <td class="right">Keterangan</td>
            <td class="left">: <input name="keterangan" /></td>
        </tr>
        <tr>
            <td class="right">Adjoining</td>
            <td class="left">
                <div id="adjoin-room" >: 
                    <?php
                    for ($i = 0; $i < count($all); $i++) {
                        if ($data_kamar['id_kamar'] == $all[$i]['id_room1'])
                            $temp = $all[$i]['id_room2'];
                        else
                            $temp = $all[$i]['id_room1'];
                        if (in_array(array('kamar_id' => $temp), $used)) {
                            ?>
                            <input type="radio" name="adjoining" id="adjoin1" value="<?php echo $temp ?>" />
                            <label for="adjoin1"><?php echo $all[$i]['nama_kamar1'] ?> - <?php echo $all[$i]['nama_kamar2'] ?> waiting list</label>
                            <?php
                        }
                        ?>
                        <input type="radio" name="adjoining" id="adjoin2" value="<?php echo $temp ?>" <?php if (isset($adjoin_data)) if ($customer != 0 && $temp == $adjoin_data['kamar_id']) echo "checked" ?> />
                        <label for="adjoin2"><?php echo $all[$i]['nama_kamar1'] ?> - <?php echo $all[$i]['nama_kamar2'] ?></label>
                        <?php
//            echo $temp;
                    }
//            print_r($used);
                    ?>
                    <input type='radio' name='adjoining' id="adjoin3" value='-' /><label for="adjoin3">none</label>
                </div>
            </td>
        </tr>   
        <tr>
            <td class="right">&nbsp;</td>
            <td class="left"><input type="submit" value="Submit" class="tombol"></td>
        </tr>
    </table>
</form>